//
//  JCStatus.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/14.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit
//微博
class JCStatus: NSObject {

    //数据id
    var id: Int = 0
    //首页数据文本
    var text: String?
    //当前微博作者
    var user: JCUser?
    //来源
    var source: String?{
        didSet{
            let sourceStr = (source! as NSString)
            let preRange = sourceStr.rangeOfString("\">")
            if preRange.location != NSNotFound {
                let lastRange = sourceStr.rangeOfString("</")
                if lastRange.location != NSNotFound{
                    let location = preRange.location + preRange.length
                    let length = lastRange.location - location
                    source = "来自\(sourceStr.substringWithRange(NSMakeRange(location, length)))"
                }
            }
        }
    }
    //创建时间
    var created_at: String?
    
    //根据当前时间,计算出来的时间显示字符串
    var createdAtStr: String{
       
            var result = ""
            let formatter = NSDateFormatter()
            //    Sat Oct 17 11:35:32 +0800 2015
            formatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
            //语言
            formatter.locale = NSLocale(localeIdentifier: "en_US")
            //            //测试数据
            //            created_at = "2015-10-22 10:10:10"
            //            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let createdDate = formatter.dateFromString(created_at!)
        
        let currentDate = NSDate()
            //今年
            if (currentDate.isThisYear(createdDate!)){
                result = "是今年"
                //今天
                if(currentDate.isToday(createdDate!)){
                    result = "是今天"
                    //获取当前时间
                    let currentDate = NSDate()
                    //获取时间差值
                    let timeInterval = currentDate.timeIntervalSinceDate(createdDate!)
                    //小于60s
                    if timeInterval<60{
                        result = "刚刚"
                    }else if timeInterval < 60*60{
                        result = "\(Int(timeInterval)/60)分钟前"
                    }else{
                        result = "\(Int(timeInterval)/(60*60))小时前"
                    }
                    
                    
                    
                    //            }else if(isYesterday(createdDate!)){  //昨天
                }else if(currentDate.isYesterday2(createdDate!)){
                    formatter.dateFormat = "昨天 HH:mm"
                    result = formatter.stringFromDate(createdDate!)
                }else{  //其他
                    formatter.dateFormat = "MM-dd HH:mm"
                    result = formatter.stringFromDate(createdDate!)
                }
            }
                //不是今年
            else{
                formatter.dateFormat = "yyyy-MM-dd"
                result = formatter.stringFromDate(createdDate!)
            }
        
        return result
    }
    
    //图片列表
    var pic_urls: [JCStatusPhotoInfo]?
    
    //转发数
    var reposts_count: Int = 0
    //评论数
    var comments_count: Int = 0
    //表态数(赞)
    var attitudes_count: Int = 0
    
    //转发微博
    var retweeted_status: JCStatus?
    
    init(dict:[String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //用户
        if (key as NSString).isEqualToString("user"){
            user = JCUser(dict: value as! [String: AnyObject])
            //转发微博
        }else if (key as NSString).isEqualToString("retweeted_status"){
        
            retweeted_status = JCStatus(dict:(value as! [String : AnyObject]))
        }else if (key as NSString).isEqualToString("pic_urls"){
            //配图数组
                pic_urls = [JCStatusPhotoInfo]()
            if let v = (value as? [[String: AnyObject]]){
                for tempV in v {
                    //转为模型
                    let photoInfo = JCStatusPhotoInfo(Dictionary: tempV)
                    //添加到图片列表模型中
                    pic_urls?.append(photoInfo)
                }
            }
        }else{
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    //  MARK: - 判断是那一段时间
    }
